package com.teamide.param;

import java.io.Serializable;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.teamide.client.ClientSession;
import com.teamide.util.RequestUtil;
import com.teamide.util.StringUtil;

/**
 * 数据载体 用于Service、Dao执行
 * 
 * @author ZhuLiang
 *
 */
public class DataParam implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final ClientSession session;

	private final JSON data;

	private JSONObject request = new JSONObject();

	private JSONObject header = new JSONObject();

	private JSONObject cache;

	private JSONObject user;

	private JSON body;

	public DataParam(JSON data) {
		this(data, null);

	}

	public DataParam(JSON data, ClientSession session) {
		this.data = data;
		this.session = session;
		if (session != null) {
			if (session.getUser() != null) {
				this.user = (JSONObject) JSON.toJSON(session.getUser());
			}
			this.cache = session.getCache();
		}

	}

	public DataParam(HttpServletRequest request, ClientSession session) {
		Map<String, String> header = RequestUtil.getRequestHeader(request);
		String requestBody = RequestUtil.getBody(request);
		Map<String, String> requestParam = RequestUtil.getRequestMap(request);
		JSON data = new JSONObject();
		JSON body = new JSONObject();
		if (!StringUtil.isEmpty(requestBody)) {
			body = (JSON) JSON.parse(requestBody);
			data = (JSON) JSON.parse(requestBody);
		} else {
			data = (JSON) JSON.toJSON(requestParam);
		}
		this.data = data;
		if (session != null) {
			if (session.getUser() != null) {
				this.user = (JSONObject) JSON.toJSON(session.getUser());
			}
			this.cache = session.getCache();
		}
		this.body = body;
		this.header = (JSONObject) JSONObject.toJSON(header);
		this.request = (JSONObject) JSONObject.toJSON(requestParam);
		this.session = session;

	}

	public JSON getData() {
		return data;
	}

	public JSONObject getRequest() {
		return request;
	}

	public void setRequest(JSONObject request) {
		this.request = request;
	}

	public JSONObject getHeader() {
		return header;
	}

	public void setHeader(JSONObject header) {
		this.header = header;
	}

	public JSON getBody() {
		return body;
	}

	public void setBody(JSON body) {
		this.body = body;
	}

	public JSONObject getCache() {
		return cache;
	}

	public void setCache(JSONObject cache) {
		this.cache = cache;
	}

	public JSONObject getUser() {
		return user;
	}

	public void setUser(JSONObject user) {
		this.user = user;
	}

	public JSONObject toVariableCache(JSONObject data) {
		JSONObject jexl = new JSONObject();
		if (data != null) {
			jexl.putAll(data);
		}
		jexl.put("$data", this.data);
		jexl.put("$body", this.body);
		if (this.request != null) {
			jexl.put("$request", this.request.clone());
		} else {
			jexl.put("$request", this.request);
		}
		if (this.header != null) {
			jexl.put("$header", this.header.clone());
		} else {
			jexl.put("$header", this.header);
		}
		if (this.cache != null) {
			jexl.put("$cache", this.cache.clone());
		} else {
			jexl.put("$cache", this.cache);
		}
		if (this.user != null) {
			jexl.put("$user", this.user.clone());
		} else {
			jexl.put("$user", this.user);
		}
		if (this.session != null) {
			try {
				jexl.put("$session", this.session.clone());
			} catch (CloneNotSupportedException e) {
				e.printStackTrace();
			}
		} else {
			jexl.put("$session", this.session);
		}
		return jexl;
	}

}
